import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormDef, FormDataSet } from './fb-design.model';
import { Subject, Observable } from 'rxjs';
import { map } from "rxjs/operators";
import { saveAs } from 'file-saver';

@Injectable()
export class FormDesignService {

    /**用于在路由的环境下与父组件通讯 */
    private routeMsgPushSource = new Subject<any>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();

    constructor(private http: HttpClient) { }

    /**获取表单树 */
    getFormTree() {
        return this.http.get('/form/api/formdef/tree').pipe(
            map((res: Response) => res['data'])
        );
    }

    /**父子组件通讯 */
    announceMission(res: any) {
        this.routeMsgPushSource.next(res);
    }

    /** 流程列表 */
    listBizProcs() {
        return this.http.get('/form/api/freeflow/listbizprocs').pipe(
            map((res: Response) => res['data'])
        );
    }

    /**表单详情**/
    showFormDefDetail(formId: number) {
        return this.http.get('/form/api/formdef/detail?id=' + formId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**保存更新表单 */
    saveUpdateFormDef(formDef: FormDef) {
        return this.http.post('/form/api/formdef/saveupdate', formDef).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**删除表单 */
    deleteFormDef(id: number) {
        return this.http.get('/form/api/formdef/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**推送表单至中央仓库 */
    pushRepo(id: number) {
        return this.http.get('/form/api/repository/push?formId=' + id).pipe(
            map((res: Response) => res['data'])
        );
    }
    /********************************************************************** */

    /** 数据源下拉框选项 */
    loadSystemInfoSelectItem() {
        return this.http.get('/form/api/interface/system/list').pipe(
            map((res: Response) => res['data'])
        );
    }

    /**新增数据源 */
    saveUpdateFormDataset(formDataSet: FormDataSet) {
        return this.http.post('/form/api/formdataset/createupdate', formDataSet).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**删除数据源 */
    deleteDataSet(id: number) {
        return this.http.get('/form/api/formdataset/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**数据源列表 */
    listDataSet(formId: number) {
        return this.http.get('/form/api/formdataset/list?formId=' + formId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**新增推送规则 */
    saveUpdateFormDataPush(dataPush: any) {
        return this.http.post('/form/api/formdatapush/createupdate', dataPush).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**删除推送规则 */
    deleteDataPush(id: number) {
        return this.http.get('/form/api/formdatapush/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**数据推送规则列表 */
    listDataPush(formId: number) {
        return this.http.get('/form/api/formdatapush/list?formId=' + formId).pipe(
            map((res: Response) => res['data'])
        );
    }

    /**获取可用组件列表 */
    getAvalibleAssemblies() {
        return this.http.get('/form/api/formdrager/assemblies').pipe(
            map((res: Response) => res['data'])
        );
    }
    /**根据表单获取组件 */
    loadAssembliesByFormId(formId: number, actDefId: number = 0) {
        return this.http.get('/form/api/formdrager/loadformassemblies?formId=' + formId + '&actDefId=' + actDefId).pipe(
            map((res: Response) => res['data'])
        );
    }
    /**增加组件 */
    formAddAssembly(formAssembly: any) {
        return this.http.post('/form/api/formdrager/createassembly', formAssembly).pipe(
            map((res: Response) => res['data'])
        );
    }
    /**移动组件 */
    moveAssembly(id: number, x: number, y: number) {
        return this.http.get('/form/api/formdrager/moveassembly?id=' + id + "&x=" + parseInt(x + '') + "&y=" + parseInt(y + '')).pipe(
            map((res: Response) => res['data'])
        );
    }
    /**清空表单 */
    clearFormDrager(formId: number) {
        return this.http.get('/form/api/formdrager/clear?formId=' + formId).pipe(
            map((res: Response) => res['data'])
        );
    }
    /** 移除单独表单 */
    removeAssemblyInst(id: number) {
        return this.http.get('/form/api/formdrager/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        );
    }
    /**保存 */
    saveFromDrager(assemblies: any[]) {
        return this.http.post('/form/api/formdrager/saveassemblies', assemblies).pipe(
            map((res: Response) => res['data'])
        );
    }
    /** 获取打印文书内容 */
    loadPrintFormContent(formId: number) {
        return this.http.get('/form/api/formdef/loadprintform?id=' + formId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 下载打印文书模板 */
    downloadPrintTemplate(fileName: string, formId: number) {
        return this.http.get('/form/api/formdef/template/download?formId=' + formId, { 'responseType': 'blob' }).pipe(
            map(res => saveAs(res, fileName + ".doc")));
    }

    /** 上传文书模板 */
    uploadPrintTemplate(file: any, formId: number) {
        return this.http.post('/form/api/formdef/template/upload?formId=' + formId, file).pipe(
            map((res: Response) => res['data'])
        )
    }

}